<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keywords" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="180">
      </el-table-column>
      <el-table-column prop="name" label="店铺名称" width="180" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="电话" width="180"  sortable>
      </el-table-column>



      <el-table-column prop="registerTime" label="注册时间" width="180"  sortable>
      </el-table-column>
      <el-table-column prop="manager.nickName" label="店铺管理员" width="180"  sortable>
      </el-table-column>
      <el-table-column prop="manager.email" label="管理员邮件" width="180"  sortable>
      </el-table-column>



      <el-table-column prop="state" label="注册状态" width="180"  sortable>

        <template slot-scope="scope">

          <span v-if="scope.row.state==1" style="color: #5e7382">待审核</span>
          <span v-if="scope.row.state==2" style="color: goldenrod">审核通过待激活</span>
          <span v-if="scope.row.state==3" style="color: green">激活成功</span>
          <span v-if="scope.row.state==4" style="color: red">驳回</span>
        </template>
      </el-table-column>

      <el-table-column prop="address" label="店铺地址" width="180"  sortable>
      </el-table-column>
      <el-table-column prop="logo" label="logo" width="180"  sortable>
        <template slot-scope="scope">
          <img v-if="scope.row.logo" :src="scope.row.logo" style="width: 50px"s/>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="500">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="danger" size="small" v-if="scope.row.state==1 || scope.row.state==4" @click="checkInfo(scope.$index, scope.row)">审核</el-button>


        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
      </el-pagination>
    </el-col>


    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="saveShopDate" :close-on-click-modal="false">
      <el-form :model="savaForm" label-width="80px" :rules="savaFormRules" ref="savaForm">
        <el-form-item label="店铺名称" >
          <el-input v-model="savaForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" >
          <el-input v-model="savaForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺地址" >
          <el-input v-model="savaForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理员" >

          <template>
            <el-select v-model="savaForm.manager" clearable value-key="id" placeholder="请选择">
              <el-option
                  v-for="item in employees"
                  :key="item.id"
                  :label="item.username"
                  :value="item">
                <span style="float: left">{{ item.username }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
              </el-option>
            </el-select>
          </template>

        </el-form-item>
        <el-form-item label="店铺logo" >
          <el-input v-model="savaForm.logo" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveShopDate = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

<!--  -->
    <el-dialog title="新增" :visible.sync="saveDate" :close-on-click-modal="false">
      <el-form :model="savaForm" label-width="80px" :rules="savaFormRules" ref="savaForm">
        <el-form-item label="店铺名称" >
          <el-input v-model="savaForm.name" disabled auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" >
          <el-input v-model="savaForm.tel" disabled auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺地址" >
          <el-input v-model="savaForm.address" disabled auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="管理员" >
          <el-input v-model="savaForm.manager.nickName" disabled auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮件" >
          <el-input v-model="savaForm.manager.email" disabled auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="店铺logo"  >


            <img v-if="savaForm.logo" :src="savaForm.logo" style="width:50px" disabled/>

        </el-form-item>
        <el-form-item label="驳回信息" >

          <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="savaForm.content">
          </el-input>

        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="returnInfoMsgTest">驳回</el-button>
        <el-button type="primary" @click.native="successInfoMsg" :loading="addLoading">审批通过</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>


export default {
  data() {
    return {
      addLoading:false,


      sels: [],//列表选中列

      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      saveShopDate: false,//新增界面是否显示

      savaFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入标识', trigger: 'blur' }
        ]
      },
      //新增界面数据
      savaForm: {

        id:null,
        name: '',
        tel:'',
        manager:{
          id:null,
          nickName:''
        },
        address:'',
        logo:'',
        oldManager:null,
        content:''

      },
      pageInfo:{
        rows:[],
        total:0
      },
      query:{
        pageSize:5,
        currentPage:1,
        keywords:''
      },
      employees:[],
      saveDate:false


    }
  },
  methods: {
    //页面刷新
    flushDate(){
      this.query.currentPage=1
      this.getShops()
    },
    //通过
    successInfoMsg(){
      this.$http.post("/shop/successInfoMsg",this.savaForm)
          .then(result=>{
            result=result.data;
            if(result.success){
              this.$message({
                message: '审核成功',
                type: 'success'
              });
            }
            this.saveDate=false;
            this.flushDate()
          })
    },

    returnInfoMsgTest(){
      console.log(this.savaForm)
      this.$http.post("/shop/returnMsg",this.savaForm)
          .then(result=>{
            result=result.data;

            if(result.success){
              this.$message({
                message: '驳回成功',
                type: 'success'
              });
            }
            this.saveDate=false;
            this.flushDate()
          })

    },

    checkInfo(index,row){
    this.saveDate=true
      this.savaForm=row


    },



    search(){
      this.query.currentPage=1;
      this.getShops();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getShops();
    },
    //获取用户列表
    getShops() {
      this.$http.post("/shop",this.query)
          .then(result=>{
            result=result.data;

            if(result.success){
              this.pageInfo=result.object;
              console.log(result.object)

            }
          })

    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {

        this.$http.delete("/shop/"+row.id)
            .then(result =>{

              result=result.data
              if(result.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '删除失败'+result.message,
                  type: 'error'
                });
              }
              // //刷新页面
              this.search();
            })
      }).catch(() => {
        this.$message({
          message: '已取消',
          type: 'info'
        });
      });
    },

    //获取所有员工
    getEmployees(){
      this.$http.get("/employee")
          .then(reslut => {
            reslut = reslut.data

            if (reslut.success) {
              this.employees = reslut.object;
            }
          })
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveShopDate = true;
      this.savaForm = Object.assign({}, row);
      this.getEmployees()
      this.savaForm.oldManager=row.manager



    },
    //显示新增界面
    handleAdd: function () {
      this.saveShopDate = true;
      this.savaForm = {
        id:null,
        name: '',
        tel:'',
        manager:{
          id:null,
          nickName:''
        },
        address:'',
        logo:'',
        oldManager:null
      };
      this.getEmployees()

    },
    //新增
    addSubmit: function () {
      this.$refs.savaForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            // if(this.savaForm.manager){
            //   this.savaForm.manager={
            //     id:null,
            //
            //   }
            // }
            this.$http.put("/shop",this.savaForm)
                .then(result =>{
                  this.listLoading=false;
                  result=result.data
                  if(result.success){
                    this.$message({
                      message: '保存成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '保存失败'+result.message,
                      type: 'error'
                    });
                  }
                  //刷新页面
                  this.saveShopDate=false;
                  this.search();
                })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;

        this.$http.patch("/shop",ids)
            .then(result =>{
              this.listLoading=false;

              result=result.data
              if(result.success){
                this.$message({
                  message: '批量删除成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '批量删除失败'+result.message,
                  type: 'error'
                });
              }
              //刷新页面
              this.search();
            })


      }).catch(() => {

      });
    }
  },

  mounted() {
    this.getShops();

  }
}

</script>

<style scoped>

</style>